/**
 * 页面加载初始化
 */
$(function(){
	var inputType = $("#input_Type option:selected");
	selectInputType(inputType[0])
});
/**
 * 输入配置点击添加事件弹出输入框界面;
 */
$("#input_config").click(function() {
	selectInputType($("#input_Type option:selected").val("list")[0]);
	$("#commonDiv").html($("#inputTypeContainer").html());
	bootbox.dialog({
		title : "输入规则配置添加",
		onEscape : true,
		message :$("#inputDomain").html() ,
		size: 'large',
		buttons : {
			button : {
				label : "添加",
				callback : function() {
					var inputType = $("#input_Type option:selected");
					setInputTextArea(inputType.val());
					return true;
				}
			}
		}
	});
});
/**
 * 设置输入配置信息;
 * @param inputType
 */
function setInputTextArea(inputType)
{
	var value = input.getValue();
	if(inputType == "list")//设置集合UrlStr
	{
		value = inputListUrlToJson(value);
	}else if(inputType == "file")//设置文件JsonStr;
	{
		value = inputFileToJson(value);
	}else if(inputType == "jdbc")//设置JdbcJsonStr;
	{
		value = inputJdbcToJson(value);
	}else if(inputType == "mongo")//设置DbJsonStr;
	{
		value = inputDbToJson(value);
	}else if(inputType == "directory")//设置DirectoryJsonStr;
	{
		value = inputDirectoryToJson(value);
	}else if(inputType == "ftp")//设置FTPJsonStr;
	{
		value = inputFTPToJson(value);
	}
	
	input.setValue(value);
}
/**
 * 输入集合Url转为JsonStr
 */
function inputListUrlToJson(inputAreaValue)
{
	var inputDivList = $(".bootbox-body div.container-input-list div.form-group");
	var inputListJsonStr = "#list\n{\n";
	var inputUrlVlue = "";
	$.each(inputDivList,function(index,obj){
		inputUrlValue = $(obj).find("input.form-control").val();
		if(inputUrlValue!=""){
			if(inputListJsonStr.lastIndexOf("list:[")<0)
			{
				inputListJsonStr +=" list:[";
			}
			inputListJsonStr +="'"+inputUrlValue+"'";
			if(index < inputDivList.length-1)
			{
				inputListJsonStr +=",";
			}
		}
		if(index == inputDivList.length-1)
		{
			inputListJsonStr +="]\n";
		}
	});
	if(inputListJsonStr.length == inputListJsonStr.lastIndexOf(",")+3)
	{
		inputListJsonStr = inputListJsonStr.substring(0,inputListJsonStr.lastIndexOf(","))+"]\n";
	}else if(inputUrlValue == "")
	{
		inputListJsonStr = inputListJsonStr.substring(0,inputListJsonStr.length-2);
	}
	inputListJsonStr +="}";
	if(inputAreaValue != "" && inputAreaValue != null){
		inputAreaValue +="\n";
	}
	inputAreaValue += inputListJsonStr;
	return inputAreaValue;
}
/**
 * 输入文件配置信息转为JsonStr
 * @param inputAreaValue
 * @returns
 */
function inputFileToJson(inputAreaValue)
{
	var path = $(".bootbox-body input[name='path']").val();
	var charEncoding = $(".bootbox-body input[name='charEncoding']").val();
	var append = $(".bootbox-body input:radio:checked").val();
	var inputFileJsonStr = "#file\n{\n";
	if(path!="")
	{
		inputFileJsonStr +=" path:'"+path+"',\n";
	}
	if(charEncoding != "")
	{
		inputFileJsonStr +=" charEncoding:'"+charEncoding+"',\n";
	}
	if(append != "")
	{
		inputFileJsonStr += " append:"+append+"\n";
	}
	inputFileJsonStr += "}";
	if(inputAreaValue != "" && inputAreaValue != null){
		inputAreaValue +="\n";
	}
	inputAreaValue +=inputFileJsonStr;
	return inputAreaValue;
}
/**
 * 输入jdbc信息转为JsonStr
 * @param inputAreaValue
 * @returns
 */
function inputJdbcToJson(inputAreaValue)
{
	var jdbcUrl = $(".bootbox-body input[name='jdbcUrl']").val();
	var username = $(".bootbox-body input[name='username']").val();
	var password = $(".bootbox-body input[name='password']").val();
	var sql = $(".bootbox-body input[name='sql']").val();
	var flag = $(".bootbox-body div.container-input-jdbc input[name='flag']").val();
	var inputJdbcJsonStr = "#jdbc\n{\n";
	if(jdbcUrl!="")
	{
		inputJdbcJsonStr +=" jdbcUrl:'"+jdbcUrl+"',\n";
	}
	if(username != "")
	{
		inputJdbcJsonStr +=" username:'"+username+"',\n";
	}
	if(password != "")
	{
		inputJdbcJsonStr +=" password:'"+password+"',\n";
	}
	if(sql != "")
	{
		inputJdbcJsonStr +=" sql:'"+sql+"',\n";
	}
	if(flag != "")
	{
		inputJdbcJsonStr +=" flag:"+flag+"\n";
	}
	if(inputJdbcJsonStr.length == inputJdbcJsonStr.lastIndexOf(",")+2)
	{
		inputJdbcJsonStr = inputJdbcJsonStr.substring(0,inputJdbcJsonStr.lastIndexOf(","))+"\n";
	}
	inputJdbcJsonStr += "}";
	if(inputAreaValue != "" && inputAreaValue != null){
		inputAreaValue +="\n";
	}
	inputAreaValue += inputJdbcJsonStr;
	return inputAreaValue;
}
/**
 * 输入数据库信息转为JsonStr
 * @param inputAreaValue
 * @returns
 */
function inputDbToJson(inputAreaValue)
{
	var host = $(".bootbox-body input[name='host']").val();
	var port = $(".bootbox-body input[name='port']").val();
	var dbName = $(".bootbox-body input[name='dbName']").val();
	var tableName = $(".bootbox-body input[name='tableName']").val();
	var batchSize = $(".bootbox-body input[name='batchSize']").val();
	var sortMethod = $(".bootbox-body input[name='sortMethod']").val();
	var flagName = $(".bootbox-body input[name='flagName']").val();
	var flag = $(".bootbox-body div.container-input-db input[name='flag']").val();
	var inputDbJsonStr = "#mongo\n{\n";
	if(host!="")
	{
		inputDbJsonStr +=" host:'"+host+"',\n";
	}
	if(port != "")
	{
		inputDbJsonStr +=" port:"+port+",\n";
	}
	if(dbName != "")
	{
		inputDbJsonStr +=" dbName:'"+dbName+"',\n";
	}
	if(tableName != "")
	{
		inputDbJsonStr +=" tableName:'"+tableName+"',\n";
	}
	if(batchSize != "")
	{
		inputDbJsonStr +=" batchSize:"+batchSize+",\n";
	}
	if(sortMethod != "")
	{
		inputDbJsonStr +=" sortMethod:'"+sortMethod+"',\n";
	}
	if(flagName != "")
	{
		inputDbJsonStr +=" flagName:'"+flagName+"',\n";
	}
	if(flag != "")
	{
		inputDbJsonStr +=" flag:"+flag+"\n";
	}
	if(inputDbJsonStr.length == inputDbJsonStr.lastIndexOf(",")+2)
	{
		inputDbJsonStr = inputDbJsonStr.substring(0,inputDbJsonStr.lastIndexOf(","))+"\n";
	}
	inputDbJsonStr += "}";
	if(inputAreaValue != "" && inputAreaValue != null){
		inputAreaValue +="\n";
	}
	inputAreaValue += inputDbJsonStr;
	return inputAreaValue;
}

/**
 * 输入Directory配置信息转为JsonStr
 * @param inputAreaValue
 * @returns
 */
function inputDirectoryToJson(inputAreaValue)
{
	var path = $(".bootbox-body input[name='directory_path']").val();
	var isSkipHidden = $(".bootbox-body input:radio:checked").val();
	var inputDirectoryJsonStr = "#directory\n{\n";
	if(path!="")
	{
		inputDirectoryJsonStr +=" path:'"+path+"',\n";
	}
	if(isSkipHidden != "")
	{
		inputDirectoryJsonStr += " isSkipHidden:"+isSkipHidden+"\n";
	}
	inputDirectoryJsonStr += "}";
	if(inputAreaValue != "" && inputAreaValue != null){
		inputAreaValue +="\n";
	}
	inputAreaValue +=inputDirectoryJsonStr;
	return inputAreaValue;
}

/**
 * 输入数据库信息转为JsonStr
 * @param inputAreaValue
 * @returns
 */
function inputFTPToJson(inputAreaValue)
{
	var server = $(".bootbox-body input[name='ftp_server']").val();
	var port = $(".bootbox-body input[name='ftp_port']").val();
	var user = $(".bootbox-body input[name='ftp_user']").val();
	var password = $(".bootbox-body input[name='ftp_password']").val();
	var path = $(".bootbox-body input[name='ftp_path']").val();
	var inputFTPJsonStr = "#ftp\n{\n";
	if(server != "")
	{
		inputFTPJsonStr +=" server:'"+server+"',\n";
	}
	if(port != "")
	{
		inputFTPJsonStr +=" port:"+port+",\n";
	}
	if(user != "")
	{
		inputFTPJsonStr +=" user:'"+user+"',\n";
	}
	if(password != "")
	{
		inputFTPJsonStr +=" password:'"+password+"',\n";
	}
	if(path != "")
	{
		inputFTPJsonStr +=" path:"+path+"\n";
	}
	inputFTPJsonStr += "}";
	if(inputAreaValue != "" && inputAreaValue != null){
		inputAreaValue +="\n";
	}
	inputAreaValue += inputFTPJsonStr;
	return inputAreaValue;
}


/**
 * 选择下拉框选择列表;
 * @param obj
 */
function selectInputType(obj)
{
	var value = $(obj).val();
	$("#input_Type option:selected").val(value);
	if("list" == value)
	{
		$("div.container-input-list").show();
		$("div.container-input-file").hide();
		$("div.container-input-jdbc").hide();
		$("div.container-input-db").hide();
		$("div.container-input-directory").hide();
		$("div.container-input-ftp").hide();
	}else if("file" == value)
	{
		$("div.container-input-list").hide();
		$("div.container-input-jdbc").hide();
		$("div.container-input-file").show();
		$("div.container-input-db").hide();
		$("div.container-input-directory").hide();
		$("div.container-input-ftp").hide();
	}else if("jdbc" == value){
		$("div.container-input-jdbc").show();
		$("div.container-input-list").hide();
		$("div.container-input-file").hide();
		$("div.container-input-db").hide();
		$("div.container-input-directory").hide();
		$("div.container-input-ftp").hide();
	}else if("mongo" == value)
	{
		$("div.container-input-jdbc").hide();
		$("div.container-input-list").hide();
		$("div.container-input-file").hide();
		$("div.container-input-db").show();
		$("div.container-input-directory").hide();
		$("div.container-input-ftp").hide();
	}else if("directory" == value)
	{
		$("div.container-input-jdbc").hide();
		$("div.container-input-list").hide();
		$("div.container-input-file").hide();
		$("div.container-input-db").hide();
		$("div.container-input-directory").show();
		$("div.container-input-ftp").hide();
	}else if("ftp" == value)
	{
		$("div.container-input-jdbc").hide();
		$("div.container-input-list").hide();
		$("div.container-input-file").hide();
		$("div.container-input-db").hide();
		$("div.container-input-directory").hide();
		$("div.container-input-ftp").show();
	}
}
/**
 * 移除集合输入框;
 * @param index
 */
function removeInputListUrl(index)
{
	$("div.container-input-list").find("div.form-group[index='"+index+"']").remove();
}
/**
 * 添加集合输入框;
 * @param obj
 */
function addInputListUrl(obj)
{
	var listContainer = $(obj).parents("div.container-input-list:first");
	var index = listContainer.find("div.form-group").length;
	var html = 
		"<div class='form-group' index='"+(index+1)+"'>"+
		    "<label class='col-sm-2 control-label'>"+
			    "<a onclick=\"removeInputListUrl('"+(index+1)+"')\" ><span class='glyphicon glyphicon-minus' ></span></a>"+
		    "</label>"+
		    "<div class='col-sm-5'>"+
		      "<input class='form-control' placeholder='Url'>"+
		    "</div>"+
	    "</div>"
	listContainer.find("div.form-group[index='"+index+"']").after(html);
}